module.exports = (sequelize, DataTypes) => {
  const User = sequelize.define('User', {
    id: {
      type: DataTypes.INTEGER,
      primaryKey: true,
      autoIncrement: true
    },
    username: {
      type: DataTypes.STRING(50),
      allowNull: false,
      unique: true,
      validate: {
        notEmpty: true
      }
    },
    token: {
      type: DataTypes.STRING(255),
      allowNull: true,
      comment: 'Coze授权令牌'
    },
    stylePreference: {
      type: DataTypes.JSON,
      allowNull: true,
      comment: '用户风格偏好，JSON格式存储'
    },
    createTime: {
      type: DataTypes.DATE,
      defaultValue: DataTypes.NOW
    }
  }, {
    tableName: 'users',
    timestamps: true,
    createdAt: 'createTime',
    updatedAt: false
  });

  User.associate = (models) => {
    // 用户与资源的一对多关系
    User.hasMany(models.Resource, {
      foreignKey: 'userId',
      as: 'resources'
    });

    // 用户与反馈的一对多关系
    User.hasMany(models.Feedback, {
      foreignKey: 'userId',
      as: 'feedbacks'
    });
  };

  return User;
};